﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Text;
using ZLDC.Common.BaseUser;
using ZLDC.Common.Estate;

namespace ZLDC.Transaction.UI.AjaxData
{
    public partial class GetUserAuto : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                if (Request.QueryString["uname"] != null)
                {
                    string result = HttpUtility.UrlDecode(Request.QueryString["uname"].Trim());
                    Response.Write(GetLikeUserName(result));
                    Response.End();
                }
            }
        }

        private String GetLikeUserName(String prefixText)
        {
            string sKey = prefixText.Replace("'", "''");
            StringBuilder sbstr = new StringBuilder();

            string sSql = "select top 50 id,name,[no], dept_id,deptName as t_name from t_user u where u.DltFlag = 0 ";
            sSql += " and (u.namepy like N'" + sKey + "%' or u.name like N'" + sKey + "%') ";

            if (Page.Request.QueryString["tname"] != null && Page.Request.QueryString["tname"].Trim() != "")
            {
                string stname = HttpUtility.UrlDecode(Page.Request.QueryString["tname"].Trim());

                if (stname == null)
                    stname = "N''";
                else
                    stname = "N'" + stname.Replace("'", "''") + "'";

                sSql += " and t_id in (select dp.id from t_zlhq_t_s_alldept dp where dp.name = " + stname + ")";
            }
            
            DataTable dt = new DataTable();
            using (_Database db = new _Database("estate"))
            {
                dt = db.Query(sSql).Tables[0];
            }
            
            for (int i = 0; i < dt.Rows.Count; i++)
            {
               // sbstr.Append(dt.Rows[i]["name"].ToString() + "[" + dt.Rows[i]["no"].ToString() + "," + dt.Rows[i]["t_name"].ToString() + "]");
                sbstr.Append(dt.Rows[i]["name"].ToString() );
                sbstr.Append("\n");
            }

            return sbstr.ToString();
           
        }
    }
}
